package com.sgb.kjwl.view.ui.adapter.self.publish;

import android.content.Context;
import android.view.View;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemLeaseStateBinding;
import com.sgb.kjwl.model.entity.self.LeaseStateEntity;
import com.sgb.kjwl.view.ui.activity.house.LeaseHousesActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.CarPublishActivity1;
import com.sgb.kjwl.view.ui.activity.self.publish.DevicePublishActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.LeaseFactoryActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.LeaseFarmActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.LeaseLandActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.LeaseMachineActivity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;

import java.util.List;

public class LeaseStateAdapter extends BaseRecycleAdapter<LeaseStateEntity> {

    private ItemCallBack itemCallBack;
    private ItemLeaseStateBinding binding;

    public LeaseStateAdapter(Context context, List<LeaseStateEntity> datas) {
        super(context, R.layout.item_lease_state, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final LeaseStateEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        binding = (ItemLeaseStateBinding) holder.getmBinding();
        binding.tvTime.setText(DateUtils.stampToDay(entity.getUpdatetime()));

        if ("1".equals(entity.getType())) {
            binding.tvType.setText("机械租赁");
        } else if ("2".equals(entity.getType())) {
            binding.tvType.setText("设备租赁");
        } else if ("3".equals(entity.getType())) {
            binding.tvType.setText("车辆租赁");
        } else if ("4".equals(entity.getType())) {
            binding.tvType.setText("房产租赁");
        } else if ("5".equals(entity.getType())) {
            binding.tvType.setText("土地租赁");
        } else if ("6".equals(entity.getType())) {
            binding.tvType.setText("工厂租赁");
        } else if ("7".equals(entity.getType())) {
            binding.tvType.setText("农机租赁");
        }

        if ("1".equals(entity.getNegotiable())) {
            binding.tvPrice.setText("面议");
        } else {
            binding.tvPrice.setText(entity.getPrice() + entity.getPriceUnit());
        }

        if ("0".equals(entity.getStatus())) {
            binding.upView.setVisibility(View.GONE);
            binding.downView.setVisibility(View.GONE);
            binding.delView.setVisibility(View.VISIBLE);
            binding.editView.setVisibility(View.VISIBLE);
            binding.ivStatus.setImageResource(R.mipmap.ic_lease_state0);
        } else if ("1".equals(entity.getStatus())) {
            binding.upView.setVisibility(View.GONE);
            binding.downView.setVisibility(View.VISIBLE);
            binding.delView.setVisibility(View.GONE);
            binding.editView.setVisibility(View.GONE);
            binding.ivStatus.setImageResource(R.mipmap.ic_lease_state1);
        } else if ("2".equals(entity.getStatus())) {
            binding.upView.setVisibility(View.VISIBLE);
            binding.downView.setVisibility(View.GONE);
            binding.delView.setVisibility(View.VISIBLE);
            binding.editView.setVisibility(View.VISIBLE);
            binding.ivStatus.setImageResource(R.mipmap.ic_lease_state2);
        } else if ("3".equals(entity.getStatus())) {
            binding.upView.setVisibility(View.GONE);
            binding.downView.setVisibility(View.GONE);
            binding.delView.setVisibility(View.VISIBLE);
            binding.editView.setVisibility(View.VISIBLE);
            binding.ivStatus.setImageResource(R.mipmap.ic_lease_state3);
        } else if ("4".equals(entity.getStatus())) {
            binding.upView.setVisibility(View.GONE);
            binding.downView.setVisibility(View.GONE);
            binding.delView.setVisibility(View.GONE);
            binding.editView.setVisibility(View.GONE);
            binding.lineView.setVisibility(View.GONE);
            binding.ivStatus.setImageResource(R.mipmap.ic_lease_state4);
        }

        binding.delView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemCallBack != null) {
                    itemCallBack.del(entity.getDataId(), entity.getType());
                }
            }
        });
        binding.upView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemCallBack != null) {
                    itemCallBack.up(entity.getDataId(), entity.getType());
                }
            }
        });
        binding.downView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemCallBack != null) {
                    itemCallBack.down(entity.getDataId(), entity.getType());
                }
            }
        });
        binding.editView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onEdit(entity.getType(), entity.getDataId());
            }
        });

        binding.llItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemCallBack != null) {
                    itemCallBack.detail(entity.getDataId(), entity.getType());
                }
            }
        });
    }

    public void setItemCallBack(ItemCallBack itemCallBack) {
        this.itemCallBack = itemCallBack;
    }

    public interface ItemCallBack {
        void del(String id, String type);

        void up(String id, String type);

        void down(String id, String type);

        void detail(String id, String type);
    }

    private void onEdit(String type, String id) {
        switch (type) {
            case "1"://机械
                LeaseMachineActivity.start(context, id);
                break;
            case "2": //设备
                DevicePublishActivity.start(context, id);
                break;
            case "3"://车辆
                CarPublishActivity1.start(context, id);
                break;
            case "4"://房产
                LeaseHousesActivity.start(context, id);
                break;
            case "5"://土地
                LeaseLandActivity.start(context, id);
                break;
            case "6"://工厂
                LeaseFactoryActivity.start(context, id);
                break;
            case "7"://农机
                LeaseFarmActivity.start(context, id);
                break;
        }
    }
}
